# -*-coding:utf-8 -*-
import requests

from . import config


class GeoApi:
    '''高德Api
    '''

    def __init__(self):
        self.baseurl = "https://restapi.amap.com/v3"
        self.key = config.GAODE_API["KEY"]

    # 1 逆地理转换
    def reverse_geotransformation(self, longitude, latitude):
        """接受经度和纬度将数据变为中文"""
        latitude = round(float(latitude), 6)
        longitude = round(float(longitude), 6)

        # 发送请求
        res = requests.get(f"{self.baseurl}/geocode/regeo?", params={
            'key': config.GAODE_API["KEY"],
            'location': "{},{}".format(longitude, latitude)
        }).json()
        return res['regeocode']['formatted_address']

    # 2 将原生gps坐标转换为高德坐标
    def change_gps_to_gd(self, longtitude, latitude):
        # 发送请求
        res = requests.get(f"{self.baseurl}/assistant/coordinate/convert?", params={
            'key': config.GAODE_API["KEY"],
            'locations': "{},{}".format(longtitude, latitude),
            'coordsys': "gps"
        }).json()

        return res['locations'].split(",")
